<?php

namespace app\common\service;


use think\Cache;
use app\model\FreshBanner as BannerModel;
use think\Db;

class Banner{


    const USER_KEY = 'banner_';
    const XCX_INDEX_ADV = 'xcx_index_adv';  //广告
    const XCX_INDEX_POP = 'xcx_index_pop';  //pop弹窗

    /**
     * 查询广告详情
     * @param int bannerID
     * @return array
     */
    public static function getBannerById($bannerID){


    }


    /**
     * 获取广告列表
     * @praram int place 位置id
     * @return array
     */
    public static function getIndexList($place){
        $key = self::XCX_INDEX_ADV . $place;
        $cache = Cache::store('redis')->get($key);
        if($cache){
            $list = json_decode($cache, true);
        }else{
            $list = Db::name('banner')->where(['status'=>1,'place'=>$place])->select();
            Cache::store('redis')->set($key, json_encode($list), 86400);
        }
        return  $list;
    }




    /**
     * 更新广告信息
     * @param int bannerID
     * @param array data
     * @return array
     */
    public static function updateBanner($data, $id){
        $newCacheKey = $oldCacheKey = "";
        if($data['place']){
            $banner = BannerModel::get(['bannerID' => $id]);
            $newCacheKey = self::XCX_INDEX_ADV . $data['place'];
            $oldCacheKey = self::XCX_INDEX_ADV . $banner['place'];
        }
        $bannerModel = new BannerModel;
        $result = $bannerModel->isUpdate(true)->save($data,  ['bannerID' => $id]);
        if($result > 0 && $newCacheKey && $oldCacheKey){
            Cache::store('redis')->clear($newCacheKey);
            Cache::store('redis')->clear($oldCacheKey);
        }
        return $result;
    }




    /**
     * 删除商品缓存
     * @param int bannerID 广告ID
     * @return array
     */
    public static function clearCache($bannerID){

    }



    /**
     * 新增用户地址
     * @param int userID 用户ID
     * @param array UserID 用户地址ID
     * @return array
     */
    public static function addUser($userID){

    }


}


?>